import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: jack_yang
 * @Date: 2024/4/21
 * @Description: addition and subtraction
 */
public class Equation {

    private final int NUM_MIN = 0;
    private final int NUM_MAX = 100;
    private final int RESULT_MIN = 0;
    private final int RESULT_MAX = 100;
    private int leftNum;
    private int rightNum;
    private int result;
    private String op;

    public int getNUM_MIN() {
        return NUM_MIN;
    }

    public int getNUM_MAX() {
        return NUM_MAX;
    }

    public int getRESULT_MIN() {
        return RESULT_MIN;
    }

    public int getRESULT_MAX() {
        return RESULT_MAX;
    }

    public int getLeftNum() {
        return leftNum;
    }

    public void setLeftNum(int leftNum) {
        this.leftNum = leftNum;
    }

    public int getRightNum() {
        return rightNum;
    }

    public void setRightNum(int rightNum) {
        this.rightNum = rightNum;
    }

    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    @Override
    public String toString() {
        return this.leftNum + op + this.rightNum + "=";
    }
    //产生指定区间的随机数
    //案例:0-100或0-50之间
    public int generateRandom(int min,int max){
        Random random = new Random();
        return random.nextInt(max-min +1)+min;
    }
    public boolean isBetween(int value,int min,int max){
        return value >= min && value <= max;
    }

    public int add(int leftNum,int rightNum){
        return leftNum + rightNum;
    }
    public int sub(int leftNum,int rightNum){
        return leftNum - rightNum;
    }

    //产生符合要求的加法算式
    public Equation generateAddEquation(){
        while(true){
            leftNum = generateRandom(NUM_MIN,NUM_MAX);
            rightNum = generateRandom(NUM_MIN,NUM_MAX);
            result = add(leftNum,rightNum);
            op = "+";
            if(isBetween(result,RESULT_MIN,RESULT_MAX)){
                break;
            }

        }
        return this;
    }

    public Equation generateSubEquation() {
        do{
            leftNum = generateRandom(NUM_MIN,NUM_MAX);
            rightNum = generateRandom(NUM_MIN,NUM_MAX);
            result = sub(leftNum,rightNum);
            op = "-";

        }while(!isBetween(result,RESULT_MIN,RESULT_MAX));
        return this;

    }


    public Equation generateRandomEquation(){
        Random random = new Random();
        if(random.nextInt(2) == 0){
            return generateAddEquation();
        }else{
            return generateSubEquation();
        }
    }

}
